package com.zisu.rabbitproducer.component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import com.zisu.rabbitproducer.Entity.User;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2021/4/30 8:39
 */
@Component
public class RabbitReceive {
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value="remote_command_response_queue",durable = "true"),
                    exchange = @Exchange(name = "rc_exchange",durable = "true",type = "topic",ignoreDeclarationExceptions = "true"),
                    key = "ACK.#"
            )
    )
    // TOPIC 模式
    //只要有消息过来都会执行这个函数
    @RabbitHandler
    public void remoteCommand(Message<?> message, Channel channel)throws Exception{
        System.out.println("==============remote_command-Ack=================");
        System.out.println("端口:8004 消费头："+message.getHeaders().get("attr1")+"|"+message.getHeaders().get("attr2"));
        byte[] bytes =(byte[])message.getPayload();
        ObjectMapper objectMapper = new ObjectMapper();
        User user = objectMapper.readValue(bytes, User.class);
        Thread.sleep(5000);
        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);

        try {
            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            e.printStackTrace();
        }
    }



}
